import { decrypt, encryption } from "@/assets/js/http";
import { mapGetters, mapActions } from "vuex";
import _ from "lodash";
import template from "./roomListTemplate";
const { render, staticRenderFns } = template;

export default {
  render,
  staticRenderFns,
  data() {
    return {
      roomList: {},
      showPwdDlg: false,
      lottery_type: 0,
      tip: "",
      showTip: false,
    };
  },
  mounted() {
    var params = {
      token: localStorage.getItem("token"),
      lottery_type: this.$route.query.lottery_type,
    };
    this.$http
      .post(
        this.urlRequest + "?m=api&c=lobbynew&a=room_info",
        encryption(JSON.stringify(params))
      )
      .then((res) => {
        if (res.status == 0) {
          this.roomList = JSON.parse(decrypt(res.data));
          console.log(this.roomList);
        }
        this.$vux.loading.hide();
      });
  },
  created() {
    this.$vux.loading.show();
  },

  computed: {
    ...mapGetters(["accountData", "roomData"]),
  },
  methods: {
    headBack: function () {
      this.$router.push({
        path: "/home",
      });
    },
    showPlDlg: function (tip, index) {
      if (this.roomList.room[index].max_number == "私密房间") {
        this.tip = "私密房赔率说明请进入房间后查看";
        this.showTip = true;
        return;
      }
      // tip = tip.replace(/\r?\n|\r/g, "<br>")
      tip = tip.replace(/\r?\n|\r|&crarr;|&#8629;/g, "<br>");
      console.log(tip);
      this.tip = tip ? tip : "暂无赔率说明";
      this.showTip = true;
    },
    goPwdRoom: function (pwd) {
      this.$vux.loading.show();
      this.$http
        .post(this.urlRequest + "?m=api&c=lobby&a=privateRoom", {
          lottery_type: this.lottery_type,
          secret_pwd: pwd,
        })
        .then((res) => {
          if (res.status == 0) {
            if (res.data.passwd == pwd) {
              //密码正确
              localStorage.setItem("MdStory", "私密房间");
              var obj = this.roomData;
              obj.plTipText = res.data.odds_exp;
              this.$store.commit("setRoomData", obj);
              this.$router.push({
                path: "/room/" + res.data.id,
                query: { lottery_type: this.lottery_type, id: res.data.id },
              });
            } else {
              this.$vux.toast.show({
                text: "房间密码错误",
              });
            }
          } else {
            this.$vux.toast.show({
              text: "房间密码错误",
            });
          }
          this.$vux.loading.hide();
        });
    },
    goRoom: function (item) {
      var that = this;
      var lottery_type = item.lottery_type;
      this.lottery_type = item.lottery_type;
      var id = item.id;
      var pwd = item.passwd;
      var maxYb = parseInt(item.max_yb);
      var lowYb = parseInt(item.low_yb);
      var tips = item.lack_tips;
      var userMoney = parseInt(this.accountData.money);
      if (pwd === "") {
        if (maxYb < userMoney || userMoney < lowYb) {
          this.$vux.toast.show({
            text: tips ? tips : "进入失败，请稍后重试",
          });
          return;
        }
        if (this.roomList.isAuthRoom != 0) {
          this.$vux.alert.show({
            content: "抱歉，您未开启当前房间权限！",
          });
          return;
        }
        if (item.status == 1) {
          this.$vux.toast.show({
            text: "该房间已关闭",
          });
          return;
        }
        var obj = this.roomData;
        obj.plTipText = item.odds_exp;
        this.$store.commit("setRoomData", obj);
        this.$router.push({
          path: "/room/" + id,
          query: { lottery_type: lottery_type, id: id },
        });
      } else {
        this.showPwdDlg = true;
      }
    },
  },
};

// WEBPACK FOOTER //
// ./src/components/room/roomList/roomList.vue
